package com.ruoyi.laiwu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.laiwu.domain.bo.LwAlarmDetailBo;
import com.ruoyi.laiwu.domain.vo.LwAlarmDetailVo;
import com.ruoyi.laiwu.domain.LwAlarmDetail;
import com.ruoyi.laiwu.mapper.LwAlarmDetailMapper;
import com.ruoyi.laiwu.service.ILwAlarmDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 告警详情Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@RequiredArgsConstructor
@Service
public class LwAlarmDetailServiceImpl implements ILwAlarmDetailService {

    private final LwAlarmDetailMapper baseMapper;

    /**
     * 查询告警详情
     */
    @Override
    public LwAlarmDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询告警详情列表
     */
    @Override
    public TableDataInfo<LwAlarmDetailVo> queryPageList(LwAlarmDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LwAlarmDetail> lqw = buildQueryWrapper(bo);
        Page<LwAlarmDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询告警详情列表
     */
    @Override
    public List<LwAlarmDetailVo> queryList(LwAlarmDetailBo bo) {
        LambdaQueryWrapper<LwAlarmDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LwAlarmDetail> buildQueryWrapper(LwAlarmDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LwAlarmDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSubId() != null, LwAlarmDetail::getSubId, bo.getSubId());
        lqw.eq(StringUtils.isNotBlank(bo.getMaxFireY()), LwAlarmDetail::getMaxFireY, bo.getMaxFireY());
        lqw.eq(StringUtils.isNotBlank(bo.getMaxFireX()), LwAlarmDetail::getMaxFireX, bo.getMaxFireX());
        lqw.eq(StringUtils.isNotBlank(bo.getTargetDistance()), LwAlarmDetail::getTargetDistance, bo.getTargetDistance());
        lqw.eq(StringUtils.isNotBlank(bo.getFireMaxTemperature()), LwAlarmDetail::getFireMaxTemperature, bo.getFireMaxTemperature());
        return lqw;
    }

    /**
     * 新增告警详情
     */
    @Override
    public Boolean insertByBo(LwAlarmDetailBo bo) {
        LwAlarmDetail add = BeanUtil.toBean(bo, LwAlarmDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改告警详情
     */
    @Override
    public Boolean updateByBo(LwAlarmDetailBo bo) {
        LwAlarmDetail update = BeanUtil.toBean(bo, LwAlarmDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LwAlarmDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除告警详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public LwAlarmDetailVo queryByAlarmId(Long subId) {
        LambdaQueryWrapper<LwAlarmDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LwAlarmDetail::getSubId,subId);
        List<LwAlarmDetailVo> lwAlarmDetailVos = baseMapper.selectVoList(wrapper);
        if(lwAlarmDetailVos.isEmpty()){
            return null;
        }
        return lwAlarmDetailVos.get(0);
    }
}
